package hedron.db.model;

import hedron.Application;
import hedron.db.DBManager;
import java.sql.Connection;
import java.sql.ResultSet;

public class Contact extends AbstractModel {

	// 0 is reserved for the user company
	private long CUSTOMER_ID = -1;
	public long TYPE_ID = 0;
	public String C_NAME = EMPTY_STR;
	public String C_BULSTAT = EMPTY_STR;
	public String C_TAX = EMPTY_STR;
	public String C_MOL = EMPTY_STR;
	public String C_ADDRESS = EMPTY_STR;
	public String C_CITY = EMPTY_STR;
	public String C_CONTRY = EMPTY_STR;
	public String C_BANK = EMPTY_STR;
	public String C_BANKACC = EMPTY_STR;
	public boolean C_DDS = false;
	public String C_EMAIL = EMPTY_STR;
	public String C_FAX = EMPTY_STR;
	public String C_NOTE1 = EMPTY_STR;
	public String C_NOTE2 = EMPTY_STR;
	public String C_TEL = EMPTY_STR;
	public String SEARCHTEXT;

	// external fields
	public String TYPE_NAME = EMPTY_STR;

	@Override
	public long getID() {
		return CUSTOMER_ID;
	}

	@Override
	public String getIDName() {
		return "CUSTOMER_ID";
	}

	@Override
	public String getTableName() {
		return "HCUSTOMER";
	}

	public Contact() {}

	public Contact(ResultSet rs) {
		if (rs == null) {
			throw new IllegalArgumentException("ResultSet might not be null");
		}
		if (!load(rs)) {
			throw new IllegalArgumentException("cannot load from ResultSet");
		}
	}

	@Override
	public boolean load(ResultSet rs) {
		DBManager db = Application.DB;
		CUSTOMER_ID = db.getLong(rs, getIDName(), -1);
		if (CUSTOMER_ID < 0) {
			return false;
		}

		TYPE_ID = db.getLong(rs, "TYPE_ID");
		C_NAME = db.getString(rs, "C_NAME");
		C_BULSTAT = db.getString(rs, "C_BULSTAT");
		C_TAX = db.getString(rs, "C_TAX");
		C_MOL = db.getString(rs, "C_MOL");
		C_ADDRESS = db.getString(rs, "C_ADDRESS");
		C_CITY = db.getString(rs, "C_CITY");
		C_CONTRY = db.getString(rs, "C_CONTRY");
		C_BANK = db.getString(rs, "C_BANK");
		C_BANKACC = db.getString(rs, "C_BANKACC");
		C_DDS = db.getBoolean(rs, "C_DDS");
		C_EMAIL = db.getString(rs, "C_EMAIL");
		C_FAX = db.getString(rs, "C_FAX");
		C_NOTE1 = db.getString(rs, "C_NOTE1");
		C_NOTE2 = db.getString(rs, "C_NOTE2");
		C_TEL = db.getString(rs, "C_TEL");

		TYPE_NAME = db.getString(rs, "TYPE_NAME");
		return true;
	}

	@Override
	public boolean isValid() {
		return isNotEmpty(C_NAME) && isNotEmpty(C_BULSTAT) && isNotEmpty(C_MOL);
	}

	@Override
	public boolean store(Connection conn) {

		StringBuilder sql = new StringBuilder();

		{
			StringBuilder searchtext = new StringBuilder();
			searchtext.append(CUSTOMER_ID).append(' ');
			searchtext.append(C_NAME).append(' ');
			searchtext.append(C_BULSTAT).append(' ');
			searchtext.append(C_TAX).append(' ');
			searchtext.append(C_MOL).append(' ');
			searchtext.append(C_ADDRESS).append(' ');
			searchtext.append(C_CITY).append(' ');
			searchtext.append(C_CONTRY).append(' ');
			searchtext.append(C_BANK).append(' ');
			searchtext.append(C_BANKACC).append(' ');
			searchtext.append(C_EMAIL).append(' ');
			searchtext.append(C_FAX).append(' ');
			searchtext.append(C_NOTE1).append(' ');
			searchtext.append(C_NOTE2).append(' ');
			searchtext.append(C_TEL).append(' ');
			if (C_DDS) {
				searchtext.append("ДДС");
			}
			SEARCHTEXT = searchtext.toString();
		}

		if (CUSTOMER_ID < 0) {// insert
			CUSTOMER_ID = Application.ID.getNewID();
			sql.append("insert into ");
			sql.append(getTableName());
			sql.append("(CUSTOMER_ID, TYPE_ID,C_NAME,C_BULSTAT,C_TAX,C_MOL,C_ADDRESS,C_CITY,C_CONTRY,C_BANK,C_BANKACC,C_DDS,C_EMAIL,C_FAX,C_NOTE1,C_NOTE2,C_TEL,SEARCHTEXT)");
			sql.append(" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			return db.ddlQuery(conn, sql.toString(), CUSTOMER_ID, TYPE_ID, C_NAME, C_BULSTAT, C_TAX, C_MOL, C_ADDRESS,
					C_CITY, C_CONTRY, C_BANK, C_BANKACC, C_DDS, C_EMAIL, C_FAX, C_NOTE1, C_NOTE2, C_TEL, SEARCHTEXT);
		} else {// update
			sql.append("update ");
			sql.append(getTableName());
			sql.append(" SET TYPE_ID = ?, C_NAME = ?, C_BULSTAT = ?, C_TAX = ?, C_MOL = ?, C_ADDRESS = ?, "
					+ "C_CITY = ?, C_CONTRY = ?, C_BANK = ?, C_BANKACC = ?, C_DDS = ?, "
					+ "C_EMAIL = ?, C_FAX = ?, C_NOTE1 = ?, C_NOTE2 = ?, C_TEL = ?, SEARCHTEXT = ?");
			sql.append(" where CUSTOMER_ID = ").append(CUSTOMER_ID);
			return db.ddlQuery(conn, sql.toString(), TYPE_ID, C_NAME, C_BULSTAT, C_TAX, C_MOL, C_ADDRESS, C_CITY,
					C_CONTRY, C_BANK, C_BANKACC, C_DDS, C_EMAIL, C_FAX, C_NOTE1, C_NOTE2, C_TEL, SEARCHTEXT);
		}
	}

	@Override
	public boolean delete(Connection conn) {
		if (CUSTOMER_ID < 1) {
			// reserved id for current user company cannot be marked as deleted
			return true;
		}
		StringBuilder sql = new StringBuilder();
		sql.append("update ");
		sql.append(getTableName());
		sql.append(" SET C_DELETED = true ");
		sql.append(" where ").append(getIDName()).append(" = ").append(getID());
		return db.ddlQuery(conn, sql.toString());
	}

}
